package com.huan.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "Filter",urlPatterns = "/*")
public class CharUTF8Filter implements Filter {
    public void init(FilterConfig config) throws ServletException {
        System.out.println("-------------------初始化过滤器对象");
    }

    public void destroy() {
        System.out.println("-------------------销毁过滤器对象");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("CharacterEncodingFilter---------执行过滤器方法");
        //防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }
}
